package routers

import (
	"errors"
	"strings"

	"github.com/labstack/echo"
	mw "github.com/labstack/echo/middleware"
	"shqsoft.cn/pas3ui/base"
)

// SetupMiddlewares 设置中间件
func SetupMiddlewares(e *echo.Echo) {
	e.Use(base.Hook())
	e.Use(base.RecoverMiddleware())
	e.Use(CanVisist())
	e.Use(mw.CORSWithConfig(mw.CORSConfig{
		AllowOrigins: []string{"*"},
		AllowMethods: []string{echo.GET, echo.HEAD, echo.PUT, echo.PATCH, echo.POST, echo.DELETE},
	}))
}

func CanVisist() echo.MiddlewareFunc {
	return func(next echo.HandlerFunc) echo.HandlerFunc {
		return func(c echo.Context) (err error) {
			req := c.Request()
			p := req.URL.Path
			if strings.Index(p, "/api") != 0 {
				return next(c)
			}
			isProce, _ := base.RedisCli.Get(ctx, "import:run").Result()
			exPaths := []string{"/api/sysname", "/api/BigTable/pieItems", "/api/Routes/All"}
			if isProce == "1" && !base.InStringSlice(p, exPaths) {
				err = errors.New("系统正在处理数据，请稍后访问")
				return c.HTML(403, err.Error())
			}
			return next(c)
		}
	}
}

type RespData struct {
	Code    uint8       `json:"code"`
	Data    interface{} `json:"data"`
	Message string      `json:"message"`
}

// SetupRoutes 设置路由
func SetupRoutes(e *echo.Echo) {
	// Routes
	e.Static("/publicstatic", "public/static")
	e.Static("/", "public")
	e.File("/", "public/index.html")
	e.File("/login", "public/index.html")
	e.File("/error", "public/index.html")
	r := e.Group("api/restricted")
	r2 := e.Group("api")
	SetupMiddlewares(e)
	config := mw.JWTConfig{
		Claims:     &UserLoginClaims{},
		SigningKey: []byte("secret"),
	}
	r.Use(mw.JWTWithConfig(config))
	r.GET("", Restricted)
	r.POST("", Restricted)
	r.POST("/Find/:DaoName/:PageNumber/:PageCount", FindRouter)
	r.POST("/FindTable/:TableName", FindTableRouter)
	r.POST("/FindAll/:DaoName", FindAllRouter)
	r.POST("/Add/:DaoName", AddRouter)
	r.POST("/Update/:DaoName", UpdateRouter)
	r2.GET("/Routes/All", RoutesRouter)
	r2.POST("/Upload/Delete/:FileName", UploadDeleteRouter)
	r.POST("/Delete/:DaoName", DeleteRouter)
	r.POST("/Deletes/:DaoName", DeletesRouter)
	r.POST("/WorkFlowDeletes/:DaoName", WorkFlowDeletesRouter)
	r2.POST("/Upload/:Name", UploadRouter)
	r2.POST("/DataMap", DataMapRouter)
	r.POST("/DataImport", DataImportRouter)
	r2.POST("/DataImportProcess", DataImportProcessRouter)
	r.POST("/DataImport/Down/:Name", DataImportDownRouter)
	r.GET("/workflow/RunInfo/:DaoName/:runID", GetFolwRunInfo)
	r.POST("/workflow/BatchSubmit/:DaoName", FolwSubmitBatch)
	r.POST("/workflow/Abort/:DaoName", FolwAbort)
	r.POST("/workflow/Activation", FolwActivation)
	r.POST("/workflow/IsManager/:permID", IsManagerRouter)
	r.POST("/SaveDatas/:DaoName", SaveDatasRouter)
	// r.POST("/AccountDiviflow/Comp", AccountCompDiviRouter)
	// r.POST("/AccountDiviflow/CompTy", AccountCompTyDiviRouter)
	// r.POST("/AccountDiviflow/Indi", AccountIndiDiviRouter)
	r2.GET("/sysname", GetSysName)
	r2.POST("/login", Login)
	r.GET("/Menu/All", MenuAllRouter)
	r.GET("/FlowDoingSum/All", FlowDoingSumRouter)
	r.GET("/currentUser", GetCurrentUser)
	// r2.GET("/SSRSReport/:PermID", SSRSReportRouter)
	// r.GET("/NtlmOptions", NtlmOptionsRouter)
	// r.GET("/ReportControls/:PermID", ReportControlsRouter)
	// r.GET("/ReportControlsUrl", GetReportRouterUrl)
	r.GET("/Menu/TreeNode", GetMenuTree)
	r.POST("/Menu/TreeNode/User", GetMenuUserTree)
	r.GET("/Menu/CheckedRole/perms/:RoleID", GetRolePermsKeys)
	r.GET("/Menu/CheckedRole/users/:RoleID", GetRoleUsersKeys)
	r.POST("/Menu/RolePerms/:RoleID", SaveRolePerms)
	r.POST("/Menu/UserPerms/:UserID", SaveUserPerms)
	r.POST("/Menu/Password", ChangePassword)
	r.POST("/Menu/InitPassword", InitializePassword)
	r.POST("/Menu/LeaveOffice", LeaveOffice)
	r.GET("/Menu/InitPasswordAll", InitializePasswordAll)
	r.GET("/UserPermissionTypes/:PermID", UserPermissionTypesRouter)
	r.POST("/ExportXlsx/:DaoName", ExportXlsxRouter)
	r.POST("/AccountDivi/:Name", AccountDiviRouter)
	r.GET("/jfdata/:ny", JfDataRouter)
	// r.POST("/AccountDivi/Indi", AccountIndiDiviQtzbRouter)
	// r.GET("/Index/All", IndexAllRouter)
	// r.POST("/ClientDiviIndi/Indi", ClientIndiDiviRouter)
	// r.POST("/ClientDiviComp/Comp", ClientCompDiviRouter)
	// r.POST("/ClientDiviMin/Min", ClientMinDiviRouter)
	// r.POST("/ClientDiviMin/MinXghm", ClientMinXghmDiviRouter)
	// r.POST("/ClientDiviMin/MinXghmLs", ClientMinXghmLsDiviRouter)
	// r.POST("/ClientDiviFana/IndiFana", ClientFanaDiviRouter)DD
	// r.POST("/ClientDiviLoan/IndiLoan", ClientLoanDiviRouter)
	// r.POST("/ClientCompDiviWorkFlow/Comp", ClientCompWorkFlowRouter)
	// r.POST("/ClientCompDiviSubmitWorkFlow/Comp", ClientCompSubmitWorkFlowRouter)
	// r.POST("/ClientCompRollWorkFlow/Comp", ClientCompRollWorkFlowRouter)
	// r.POST("/CompWorkFlowStep/Comp", CompWorkFlowStep)
	// r.POST("/ClientDiviWorkFlow/Indi", ClientIndiWorkFlowRouter)
	// r.POST("/ClientDiviSubmitWorkFlow/Indi", ClientIndiSubmitWorkFlowRouter)
	// r.POST("/ClientIndiRollWorkFlow/Indi", ClientIndiRollWorkFlowRouter)
	// r.POST("/IndiWorkFlowStep/Indi", IndiWorkFlowStep)
	// r.POST("/TIndiWorkFlowStep/Indi", TIndiWorkFlowStep)
	// r.POST("/TCompWorkFlowStep/Comp", TCompWorkFlowStep)
	// r.POST("/UserDiviChange/Divi", UserDiviChange)
	r.GET("/Menu/CheckedUser/perms/:UserID", GetUserPermsKeys)
	r.GET("/Menu/CheckedUser/roles/:UserID", GetUserRolesKeys)
	r.POST("/Menu/UserPerms/:UserID", SaveUserPerms)
	// r.POST("/UpdateSet/FapuZsfl", UpdateFapuZsfl)
	// r.POST("/UpdateSet/TYYWFtp", UpdateTyywFtp)
	// r.GET("/WorkFlowComp/Comp", WorkFlowCompRouter)
	// r.GET("/WorkFlowIndi/Indi", WorkFlowIndiRouter)
	// r.GET("/DiviWorkFlowGetLogin", GetLogin)
	// r.POST("/AccountDivi/IndiYgjsTz", IndiYgjsTz)
	// r.POST("/AccountDivi/dfdw", AccountDfdwDiviRouter)
	// r.POST("/AccountDivi/Gjyw", AccountGjywDiviQtzbRouter)
	r.POST("/UpDiviProcess/Divi", DataProcessRouter)
	r.POST("/UpDiviProcess/UpdateZSRQ", UpdateZSRQRouter)
	r.POST("/UpDiviProcess/DefaultDate", DefaultDateRouter)
	// r.POST("/CreateWorkFlow/UserNewOrgan", UserCreateWorkFlowRouter)
	// r.POST("/UserOrganWorkFlowStep/UserOrgan", UserStepWorkFlowRouter)
	// r.POST("/UserOrganSubmitWorkFlow/UserOrgan", UserSubmitWorkFlowRouter)
	// r.POST("/UserOrganDiviSel/UserOrgan", UserImportDiviSelRouter)
	r.POST("/UserOrganUpdate/UserOrgan", UserOrganUpdateRouter)
	// r.POST("/AlterSaveCompCile/Qylx", AccountCompClieQylxRouter)
	// r.POST("/HomePage/All", HomeRequestAllRouter)
	// r.POST("/HomePage/2", HomeRequest2Router)
	// r.POST("/BI/Query", api.Query)
	// r2.POST("/BI/Export", api.Export)
	// r.POST("/ReportParamSearch", ReportParamSearchRouter)
	r.POST("/report/info", ReportInfoRouter)
	r.POST("/report/listData", ReportDataRouter)
	r.POST("/report/export", ReportExportRouter)
	r.POST("/report/chartCardAll", ChartCardAllRouter)
	r.POST("/report/chartCard", ChartCardRouter)

	// r.POST("/CompJFXS/Delete", CompJFXSDeleteRouter)
	// r.POST("/CompJFXS/Add", CompJFXSAddRouter)
	// r.POST("/CompEditInrt/Add", CompEditInrtAddRouter)
	// r.POST("/CompEditInrt/Edit", CompEditInrtEditRouter)
	// r.POST("/Other/Kmtx", KmtxRouter)
	// r.POST("/ClientDiviMin/setJfql", setJfqlRouter)
	// r.POST("/Comp/compSetJfql", compSetJfqlRouter)

	// r.POST("/UseLog", SetUseLogRouter)
	r.POST("/UpIndiClieIsbl/clie", UpIndiClieIsblRouter)
	r.POST("/UpIndiClieIsbl/clie1", UpIndiClieIsbl1Router)
	r.POST("/UpCompClieblgm/clie", UpCompClieblgmRouter)
	r.POST("/DelClieblgm/clie", DelClieblgmRouter)
	r.POST("/AddCompbldkClie/dgh", AddCompbldkClieDghRouter)
	r.POST("/DelCompbldkClie/dgh", DelCompbldkClieDghRouter)
	r.POST("/AddCompbldkClie/grh", AddCompbldkClieGrhRouter)
	r.POST("/AddCompAccoJfxs", AddCompAccoJfxsRouter)
	r.POST("/DeleteCompAccoJfxs", DeleteCompAccoJfxsRouter)
	r.POST("/DelCompbldkClie/grh", DelCompbldkClieGrhRouter)
	r2.GET("/BigTable/pieItems", BigTabColPieItemsRouter)
	r.POST("/ygddFsChange/:frid", ygddFsChangeRouter)
	// r.GET("/SelConfUpdt/blzt", SelConfUpdtRouter)
	// r.POST("/UpConfUpdt/blzt", UpConfUpdtRouter)
	// r2.GET("/homeFirst", HomeFirstRouter)
	r.POST("/excelRept/getReptData", GetReptDataRouter)
	r.POST("/excelRept/exportRept", ExportReptRouter)

	r2.POST("/resoft/data/jurisdiction", JurisdictionRouter)
	r2.POST("/resoft/data/apply", ApplyRouter)
	r2.POST("/resoft/data/broken", BrokenRouter)

	r.POST("/AddCompCdzy", AddCompCdzyRouter)
	r.POST("/DeleteCompCdzy", DeleteCompCdzyRouter)
	r.POST("/GetSjcjzt", GetSjcjzttRouter)
	r.POST("/SetSjcjzt", SetSjcjzttRouter)
	r.POST("/SaveZhtCzr", SaveZhtCzrRouter)
	r.POST("/SaveJydwCzr", SaveJydwCzrRouter)
	r.POST("/AddZhtCzzt", AddZhtCzztRouter)
	r.POST("/AddJydwCzzt", AddJydwCzztRouter)
	r.POST("/AddCompFlrql", AddCompFlrqlRouter)
	r.POST("/DeleteCompFlrql", DeleteCompFlrqlRouter)
	r.POST("/SaveFxglCzr", SaveFxglCzrRouter)
	r.POST("/AddFxglCzzt", AddFxglCzztRouter)
	r.POST("/AddTzje", AddTzjeRouter)
	//年终业绩绩效调整
	r.POST("/AddNzTzje", AddNzTzjeRouter)

	r.POST("/AddLbjxJxbz", AddLbjxJxbzRouter)
	r.POST("/InsertLbjx", InsertLbjxRouter)
	r.POST("/AddLbjxJbxs", AddLbjxJbxsRouter)
	r.POST("/InsertJbxs", InsertJbxsRouter)

	r.POST("/SetJxsdcz", SetJxsdczRouter)
	r.POST("/InserJxsdzt", InserJxsdztRouter)

}
